//
//  ViewController.m
//  03-UIGestureRecongnizer
//
//  Created by dllo on 16/7/18.
//  Copyright © 2016年 danming. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 手势识别器
    // 分为七种
#pragma mark - 轻拍 (tap)
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 设置点击两次触发轻拍手势的事件
    tap.numberOfTapsRequired = 2;
    // 设置几个手指同时作用才能触发轻拍手势的事件, 默认为1
    tap.numberOfTouchesRequired = 2;
    // 将手势添加到view上
    [self.view addGestureRecognizer:tap];
    
#pragma mark - 长按 (longPress)
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    // 长按几秒后开始, 默认是0.5秒
    longPress.minimumPressDuration = 1;
    // 默认10像素(长按失败前, 最大的移动距离)(也可以理解为在长按的时候能偏移的距离)
    longPress.allowableMovement = 100;
    [self.view addGestureRecognizer:longPress];
#pragma mark - 清扫(swipe)
#if 0
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    [self.view addGestureRecognizer:swipe];
    // 设置清扫的方向(是个枚举值, 向右划, 向左划, 向上滑, 向下划), 可以指定多个方向(前提多个方向所触发的行为要相同)
    // 如果要使不同方向清扫所触发的行为不同, 则需要创建多个清扫的手势.
    // 左右 与 上下有冲突
    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
#endif
#pragma mark - 练习imageView添加清扫手势换图

    // 创建imageView
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 50, 200, 500)];
    _imageView.image = [UIImage imageNamed:@"02.jpg"];
    [self.view addSubview:_imageView];
    // 开启用户交互
    _imageView.userInteractionEnabled = YES;
#if 0
    // 创建一个向右清扫的手势
    UISwipeGestureRecognizer *swipeImageRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    [_imageView addGestureRecognizer:swipeImageRight];
    // 创建一个向左清扫的手势
    UISwipeGestureRecognizer *swipeImageLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    [_imageView addGestureRecognizer:swipeImageLeft];
    // 设置清扫手势为向左
    swipeImageLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    // 设置一个向右的清扫手势
    UISwipeGestureRecognizer *swipeImageDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    [_imageView addGestureRecognizer:swipeImageDown];
    swipeImageDown.direction = UISwipeGestureRecognizerDirectionDown;
    // 设置一个向左的清扫手势
    UISwipeGestureRecognizer *swipeImageUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeImage:)];
    [_imageView addGestureRecognizer:swipeImageUp];
    swipeImageUp.direction = UISwipeGestureRecognizerDirectionUp;
    // 按位枚举, 四个选项 -> 1 1 1 1
    // 全选的情况  1(3) 1(2) 1(1) 1(0)
#endif
#pragma mark - 平移(Pan)
    // 创建一个平移手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [_imageView addGestureRecognizer:pan];

#pragma mark - 捏合(pinch)
    UIPinchGestureRecognizer *pinchh = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [_imageView addGestureRecognizer:pinchh];    
#pragma mark - 旋转(rotation)
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [_imageView addGestureRecognizer:rotation];
#pragma mark - 屏幕边缘手势
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanAction:)];
    
    screenEdgePan.edges = UIRectEdgeLeft | UIRectEdgeRight;
    [self.view addGestureRecognizer:screenEdgePan];
    
}
#pragma mark - 实现轻拍手势的方法
- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}
#pragma mark - 实现长按手势的方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    // state后接的是枚举值
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
}
#pragma mark - 实现清扫手势方法
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {
    NSLog(@"清扫");
}
#pragma mark - 实现练习方法
- (void)swipeImage:(UISwipeGestureRecognizer *)swipe {
    // 利用按位枚举判断, 当为1&1时为1, 其他都为0
    if (swipe.direction & UISwipeGestureRecognizerDirectionLeft) {
        _imageView.image = [UIImage imageNamed:@"02.jpg"];
        
        NSLog(@"right = %ld", swipe.direction);
    } else if (swipe.direction & UISwipeGestureRecognizerDirectionRight) {
        _imageView.image = [UIImage imageNamed:@"01.jpg"];
        NSLog(@"left = %ld", swipe.direction);
    } else if (swipe.direction & UISwipeGestureRecognizerDirectionDown) {
        _imageView.image = [UIImage imageNamed:@"03.jpg"];
        NSLog(@"down = %ld", swipe.direction);
    } else if (swipe.direction & UISwipeGestureRecognizerDirectionUp) {
        _imageView.image = [UIImage imageNamed:@"04.jpg"];
        NSLog(@"up = %ld", swipe.direction);
    }
    // switch语句, 根据swip.direction来判断
//    switch (swipe.direction) {
//        case UISwipeGestureRecognizerDirectionRight:
//             _imageView.image = [UIImage imageNamed:@"02.jpg"];
//            NSLog(@"right = %ld", swipe.direction);
//            break;
//        case UISwipeGestureRecognizerDirectionLeft:
//            _imageView.image = [UIImage imageNamed:@"01.jpg"];
//            NSLog(@"left = %ld", swipe.direction);
//            break;
//        case UISwipeGestureRecognizerDirectionDown:
//            _imageView.image = [UIImage imageNamed:@"03.jpg"];
//            NSLog(@"down = %ld", swipe.direction);
//            break;
//        case UISwipeGestureRecognizerDirectionUp:
//            _imageView.image = [UIImage imageNamed:@"04.jpg"];
//            NSLog(@"up = %ld", swipe.direction);
//        default:
//            break;
//    }
    
}
#pragma mark - 实现平移的方法
- (void)panAction:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateBegan) {
        // 平移开始
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        // 平移中
        // 平移手势, 根据其他view上移动的过程, 得到最终点
        CGPoint point = [pan translationInView:pan.view];
        // 根据Point使pan.view移动
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        // pan移动的点置空
        [pan setTranslation:CGPointZero inView:pan.view];
         NSLog(@"%@", NSStringFromCGPoint(point));
    } else if (pan.state == UIGestureRecognizerStateEnded) {
        // 平移结束(使平移后的view归位)
//        pan.view.transform = CGAffineTransformIdentity;
    }
}
#pragma mark - 实现捏合的手势方法
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
    NSLog(@"%f", pinch.scale);
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    // 设置缩放倍数
    pinch.scale = 1;
}
#pragma mark - 实现旋转手势方法
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    // 旋转的弧度置为0
    rotation.rotation = 0;
}
#pragma mark - 实现屏幕边缘手势方法
- (void)screenEdgePanAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {
    NSLog(@"屏幕边缘");
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
